/**
 * Project Name:dsrs-systemservice-web
 * File Name:UserGroupController.java
 * Package Name:com.bwda.dsrs.systemservice.controller;
 * Date:2019年4月28日
 * Copyright (c) 2019, 江苏保旺达软件有限公司 All Rights Reserved.
 */
package com.bwda.dsrs.systemservice.controller;


import com.bwda.dsrs.base.centerbase.response.CentreCutPageResponse;
import com.bwda.dsrs.base.centerbase.response.CentreListResponse;
import com.bwda.dsrs.base.centerbase.response.ResponseEntity;
import com.bwda.dsrs.base.util.CopyUtil;
import com.bwda.dsrs.systemservice.annotations.NeedRole;
import com.bwda.dsrs.systemservice.aspect.LogTypeEnum;
import com.bwda.dsrs.systemservice.aspect.Loggable;
import com.bwda.dsrs.systemservice.domain.condition.usergroup.UserGroupCondition;
import com.bwda.dsrs.systemservice.domain.exception.SystemServiceException;
import com.bwda.dsrs.systemservice.domain.form.usergroup.UserGroupCreateForm;
import com.bwda.dsrs.systemservice.domain.form.usergroup.UserGroupQueryForm;
import com.bwda.dsrs.systemservice.domain.form.usergroup.UserGroupUpdateForm;
import com.bwda.dsrs.systemservice.domain.po.usergroup.UserGroupPo;
import com.bwda.dsrs.systemservice.domain.vo.usergroup.UserGroupVo;
import com.bwda.dsrs.systemservice.service.UserGroupService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

/**
 * @author liangjianyong
 */
@RestController
@RequestMapping("/s/usergroup")
@Api(value = "/s/usergroup", tags = "用户和自定义组关联关系")
public class UserGroupController extends BaseController {

    @Autowired
    private UserGroupService userGroupService;

    @ApiOperation(value = "查询关联记录", notes = "查询关联记录", httpMethod = "GET")
    @Loggable(descp = "查询关联记录",type = LogTypeEnum.QUERY,include = "")
    @RequestMapping(value = "/query", method = {RequestMethod.GET, RequestMethod.POST})
    public ResponseEntity<UserGroupVo> query(@ApiParam(value = "关联关系ID", required = true) @RequestParam Integer id) throws SystemServiceException {
        UserGroupPo po = userGroupService.queryWithValid(id);
        UserGroupVo vo = CopyUtil.transfer(po, UserGroupVo.class);
        return getSuccessResult(vo);
    }

    @ApiOperation(value = "查询关联记录数量", notes = "查询关联记录数量", httpMethod = "GET")
    @Loggable(descp = "查询关联记录数量",type = LogTypeEnum.QUERY,include = "")
    @RequestMapping(value = "/queryCount", method = {RequestMethod.GET, RequestMethod.POST})
    public ResponseEntity<Integer> queryCount(@ModelAttribute @Valid UserGroupQueryForm form) throws SystemServiceException {
        UserGroupCondition condition = this.getConditionByQueryForm(form);
        int count = userGroupService.queryCount(condition);
        return getSuccessResult(count);
    }

    @ApiOperation(value = "查询关联记录", notes = "查询关联记录", httpMethod = "GET")
    @Loggable(descp = "查询关联记录",type = LogTypeEnum.QUERY,include = "")
    @RequestMapping(value = "/queryList", method = {RequestMethod.GET, RequestMethod.POST})
    public ResponseEntity<CentreListResponse<UserGroupVo>> queryList(@ModelAttribute @Valid UserGroupQueryForm form) throws SystemServiceException {
        UserGroupCondition condition = this.getConditionByQueryForm(form);
        List<UserGroupPo> poList = userGroupService.queryList(condition);
        List<UserGroupVo> voList = CopyUtil.transfer(poList, UserGroupVo.class);
        return getSuccessResult(getListResponse(voList));
    }

    @ApiOperation(value = "查询关联记录列表（分页）", notes = "查询关联记录列表（分页）", httpMethod = "GET")
    @Loggable(descp = "查询关联记录列表（分页）",type = LogTypeEnum.QUERY,include = "")
    @RequestMapping(value = "/queryPageList", method = {RequestMethod.GET, RequestMethod.POST})
    public ResponseEntity<CentreCutPageResponse<UserGroupVo>> queryPageList(@ModelAttribute @Valid UserGroupQueryForm form) throws SystemServiceException {
        UserGroupCondition condition = this.getConditionByQueryForm(form);
        List<UserGroupVo> voList = new ArrayList<>();
        int count = userGroupService.queryCount(condition);
        if (count > 0) {
            List<UserGroupPo> poList = userGroupService.queryList(condition);
            voList = CopyUtil.transfer(poList, UserGroupVo.class);
        }
        return getSuccessResult(getPageResponse(form, count, voList));
    }
    @NeedRole("sysadminrole")
    @ApiOperation(value = "添加关联记录", notes = "添加关联记录", httpMethod = "POST")
    @Loggable(descp = "添加关联记录",type = LogTypeEnum.CREATE,include = "")
    @RequestMapping(value = "/add", method = {RequestMethod.GET, RequestMethod.POST})
    public ResponseEntity<UserGroupVo> add(@ModelAttribute @Valid UserGroupCreateForm form) throws SystemServiceException {
        UserGroupPo po = CopyUtil.transfer(form, UserGroupPo.class);
        userGroupService.insert(po);
        UserGroupVo vo = CopyUtil.transfer(po, UserGroupVo.class);
        return getSuccessResult(vo);
    }
    @NeedRole("sysadminrole")
    @ApiOperation(value = "修改关联记录", notes = "修改关联记录", httpMethod = "POST")
    @Loggable(descp = "修改关联记录",type = LogTypeEnum.UPDATE,include = "")
    @RequestMapping(value = "/update", method = {RequestMethod.GET, RequestMethod.POST})
    public ResponseEntity update(@ModelAttribute @Valid UserGroupUpdateForm form) throws SystemServiceException {
        UserGroupPo po = CopyUtil.transfer(form, UserGroupPo.class);
        userGroupService.update(po);
        return getSuccessResult();
    }
    @NeedRole("sysadminrole")
    @ApiOperation(value = "删除关联记录", notes = "删除关联记录", httpMethod = "POST")
    @Loggable(descp = "删除关联记录",type = LogTypeEnum.DELETE,include = "")
    @RequestMapping(value = "/delete", method = {RequestMethod.GET, RequestMethod.POST})
    public ResponseEntity delete(@ApiParam(value = "关联关系ID", required = true) @RequestParam Integer id) throws SystemServiceException {
        userGroupService.delete(id);
        return getSuccessResult();
    }

    /**
     * UserGroupQueryForm转换为UserGroupCondition
     *
     * @param form
     * @return
     */
    private UserGroupCondition getConditionByQueryForm(UserGroupQueryForm form) {
        UserGroupCondition condition = CopyUtil.transfer(form, UserGroupCondition.class);
        return condition;
    }

}